function e(e) {
    return (e = e.toString())[1] ? e : "0" + e;
}

function t(t, a) {
    var r = [ "Y", "M", "D", "h", "m", "s" ], n = [], u = new Date(t);
    n.push(u.getFullYear()), n.push(e(u.getMonth() + 1)), n.push(e(u.getDate())), n.push(e(u.getHours())), 
    n.push(e(u.getMinutes())), n.push(e(u.getSeconds()));
    for (var s in n) a = a.replace(r[s], n[s]);
    return a;
}

function a(e, t, a) {
    var r = new Date();
    r.setDate(r.getDate() - t), r.setHours(r.getHours() - e);
    var n = r.getFullYear(), u = r.getMonth() + 1, s = r.getDate(), i = r.getHours(), g = r.getMinutes(), o = (r.getMinutes() + "")[1] ? g : "0" + g;
    return e > 0 ? n + "/" + u + "/" + s + " " + i + ":" + o : n + "/" + u + "/" + s + " 00:00:00";
}

module.exports = {
    formatTime: t,
    getFormattedTime: function(e) {
        if (!e || isNaN(e) || 0 == e) return "0秒";
        var t = parseInt(e / 3600), a = parseInt((e - 3600 * t) / 60), r = parseInt((e - 3600 * t) % 60);
        if (e > 0) {
            if (t > 0) {
                var n = parseInt(t / 24);
                return n > 0 ? n + "天" + t % 24 + "小时" : t + "小时" + a + "分";
            }
            return a > 0 ? a + "分" + r + "秒" : r + "秒";
        }
        return "NaN";
    },
    caluateTime: function(e) {
        var t, r = new Date(), n = r.getFullYear(), u = r.getMonth() + 1, s = r.getDate();
        r.getHours(), r.getMinutes(), r.getSeconds();
        switch (e) {
          case 1:
            t = new Date(n + "/" + u + "/" + s + " 00:00:00");
            break;

          case 2:
            t = a(0, 1);
            break;

          case 3:
            t = a(0, 2);
            break;

          case 4:
            t = a(1, 0);
        }
        return Date.parse(t);
    },
    getTime: function(e, t, a) {
        var r = new Date();
        r.setDate(r.getDate() - t), r.setHours(r.getHours() - e);
        var n = r.getFullYear(), u = r.getMonth() + 1, s = r.getDate(), i = r.getHours(), g = r.getMinutes(), o = (r.getMinutes() + "")[1] ? g : "0" + g, D = (r = r.getDate() + "")[1] ? s : "0" + s;
        return a ? i + ":" + o : n + "/" + u + "/" + D;
    },
    getDistance: function(e, t) {
        function a(e) {
            return e * Math.PI / 180;
        }
        if (e && t) {
            var r = a(e.lat), n = a(t.lat), u = r - n, s = a(t.lng) - a(e.lng), i = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(u / 2), 2) + Math.cos(r) * Math.cos(n) * Math.pow(Math.sin(s / 2), 2)));
            return i *= 6378.137, 1e3 * (i = Math.round(1e4 * i) / 1e4);
        }
    },
    statusTime: function(e) {
        var t = (Date.parse(new Date()) - e) / 3600 / 24;
        if (t <= 60 && 0 == t) {
            var a = (Date.parse(new Date()) - e) / 3600;
            a < 1 && Date.parse(new Date());
        }
    },
    addTime: function(e) {
        var t = new Date();
        return t.setDate(t.getDate() - e), Date.parse(t);
    },
    initYearMonthDay: function(e, a, r) {
        e = e || 0, a = a ? e : 0, r = r || 0;
        var n = (n = function(t) {
            var n = t.getFullYear() + e, u = (u = t.getMonth() + 1 + a) < 10 ? "0" + u : u, s = t.getDate() + r;
            return n + "-" + u + "-" + (s = s < 10 ? "0" + s : s);
        }(new Date())).split("-");
        return t((n = new Date(Number(n[0]), Number(n[1]) - 1, Number(n[2]))).setDate(n.getDate()), "Y-M-D");
    }
};